// ./models/User.js
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  userId: { type: String, required: true, unique: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  nickname: { type: String, required: true },
  role: { type: String, enum: ['girl', 'boy'], default: 'girl' },
  // 新增性别和个人简介字段
  gender: { 
    type: String, 
    enum: ['男', '女', '其他'], 
    default: '其他' 
  },
  bio: { 
    type: String, 
    maxLength: 200,  // 限制简介最大长度为200字符
    default: '这个人很懒，什么都没写~' 
  },
  avatar: { type: String, default: 'upload/images/default_head.jpg' },
  friends: [{
    userId: { type: String, required: true },
    remark: { type: String, default: '' }
  }],
  backgroundImage: {
    type: String,
    default: '' // 不设置时为空，前端按原样显示
  }
});

userSchema.statics.addFriend = async function (userId, friendId) {
  if (userId === friendId) return false;
  const user = await this.findOne({ userId });

  if (!user || user.friends.map(friend => friend.userId).includes(friendId)) return false;
  user.friends.push({ userId: friendId });
  await user.save();
  return true;
};


module.exports = mongoose.model('User', userSchema);
